<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Shows the login form.
 * @access public
 * @param int $company_id ID of the company from which the user is logging in.
 * @param array $message An array containing the messages that will be displayed on the user's screen.
 */
function goto_login($company_id, $message = NULL)
{
    $ci = &get_instance();
    $ci->load->model('backend-companies/company_model');
    
    $data                   = array();
    $data['company_id']     = $company_id;
    $data['company_logo']   = $ci->company_model->attribute( array('id' => $company_id), 'image')->get();
    $data['message']        = $message;
    
    $ci->template
            ->title('Login')
            ->set_layout('minimum')
            ->set_partial('notifications', 'layouts/notifications')
            ->build('login', $data);
}

/**
 * Displays the forgot password form.
 * @param type $message The message that will be displayed on the user's screen.
 * @access public
 */
function goto_forgot_password($message) {
    $ci = &get_instance();
    $ci->data['message'] = $message;

    // set stylesheet, script, template and view
    $ci->template
            ->title('Forgot Password')
            ->set_layout('login')
            ->set_partial('notifications', 'notifications')
            ->build('forgot_password', $ci->data);
}

/**
 * Generates an 8 character string as a csrfkey.
 * @access public
 */
function get_csrf_nonce()
{
    $ci = &get_instance();
    $ci->load->helper('string');
    
    $key    = random_string('alnum', 8);
    $value  = random_string('alnum', 20);
    
    $ci->session->set_flashdata('csrfkey', $key);
    $ci->session->set_flashdata('csrfvalue', $value);

    return array($key => $value);
}

/**
 * Validates the csrfkey for protecting the user's session.
 * @access public
 * @return boolean
 */
function valid_csrf_nonce()
{
    $ci = &get_instance();
    
    if ($ci->input->post($ci->session->flashdata('csrfkey')) !== FALSE &&
            $ci->input->post($ci->session->flashdata('csrfkey')) == $ci->session->flashdata('csrfvalue'))
    {
        return TRUE;
    } else
    {
        return FALSE;
    }
}

/* End of file authentication_helper.php */
/* Location: ./application/modules/backend-authentication/helpers/authentication_helper.php */